//  @ Project : GagalPanen
//  @ File Name : gagalpanen.cpp
//  @ Date : 3/9/2011
//  @ Author : Denis
//
//

#include <fstream>
#include <cstring>
#include "gagalpanen.hpp"
#include "view.hpp"
#include "game.hpp"

GagalPanen::GagalPanen() {
}

void GagalPanen::newGame(std::string username) {
  Game* game = new Game(username, username + ".gag");
  game->start();
  delete game;
  View::drawTitle();
  return;
}

void GagalPanen::loadGame(std::string filename) {
  std::ifstream is(filename.c_str());
  Game* game = new Game(is, filename);
  is.close();
  game->start();
  delete game;
  View::drawTitle();
  return;
}

void GagalPanen::start() {

  View::drawTitle();
  while (true) {
    char* buf = new char[1500];
    scanf("%[^#]#", buf);
    std::string helper = buf;
    while ((int)helper.size() > 0 && isspace(helper[0])) {
      helper.erase(helper.begin());
    }
    strcpy(buf, helper.c_str());
    buf = strtok(buf," ");
    if (!strcmp(buf,"new")){
      //new game
      if ((buf = strtok(NULL," "))) {
        newGame(buf);
      } else {
        std::cout << "Parameter for new is : new <character name>#" << std::endl;
      }
    } else if (!strcmp(buf, "load")) {
      // load game
      if ((buf = strtok(NULL," "))) {
        loadGame(buf);
      } else {
        std::cout << "Parameter for load is : load <file name>#" << std::endl;
      }
    } else if (!strcmp(buf, "exit")) {
      // exit game
      std::cout << "Thank you for playing. Play again!\n";
      return;
    } else {
      std::cout << "Input not recognized. Try new, load, or exit.\n";
    }
  }
  return;
}

